package com.caigou.modules.personal.viewmodel

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.caigou.comm.base.bean.UserBean
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.utils.Logger
import com.caigou.modules.personal.bean.GrowthVoBean
import com.caigou.modules.personal.repository.PersonalRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import java.io.File
import javax.inject.Inject

/**
 * @author #Suyghur.
 * Created on 2021/10/09
 */
@HiltViewModel
class PersonalInfoViewModel @Inject constructor(private val repository: PersonalRepository) : ViewModel() {

    val passwordLiveData = MutableLiveData<Resource<String>>()
    val nickNameLiveData = MutableLiveData<Resource<UserBean>>()
    val uploadAvatarLiveData = MutableLiveData<Resource<String>>()
    val realNameLiveData = MutableLiveData<Resource<UserBean>>()
    val signatureLiveData = MutableLiveData<Resource<UserBean>>()
    val growthVoLiveData = MutableLiveData<Resource<GrowthVoBean>>()

    fun modifyPwd(userId: String, oldPwd: String, newPwd: String) {
        passwordLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.modifyPwd(userId, oldPwd, newPwd)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.msg)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<String>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    passwordLiveData.value = it
                }
        }
    }

    fun uploadAvatar(userId: String, file: File) {
        uploadAvatarLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.uploadAvatar(userId, file)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.msg)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<String>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    uploadAvatarLiveData.value = it
                }
        }
    }

    fun modifyNickName(userId: String, nickName: String) {
        nickNameLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.modifyNickName(userId, nickName)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }.catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<UserBean>(-1, message)
                    Logger.e("catch ${it.message}")
                }.collectLatest {
                    nickNameLiveData.value = it
                }
        }
    }

    fun modifyRealName(userId: String, realName: String) {
        realNameLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.modifyRealName(userId, realName)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }.catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<UserBean>(-1, message)
                    Logger.e("catch ${it.message}")
                }.collectLatest {
                    realNameLiveData.value = it
                }
        }
    }

    fun modifySignature(userId: String, signature: String) {
        signatureLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.modifySignature(userId, signature)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }.catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<UserBean>(-1, message)
                    Logger.e("catch ${it.message}")
                }.collectLatest {
                    signatureLiveData.value = it
                }
        }
    }

    fun getGrowthValueList(page: Int, limit: Int, userId: String) {
        growthVoLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getGrowthValueList(page, limit, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }.catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<GrowthVoBean>(-1, message)
                    Logger.e("catch ${it.message}")
                }.collectLatest {
                    growthVoLiveData.value = it
                }
        }
    }
}